package com.crm.controller.before_sale;

import com.crm.pojo.Offer;
import com.crm.pojo.OfferDetail;
import com.crm.service.before_sale.OfferService;
import com.crm.vo.OfferDetailsVO;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报价
 */
@Controller
public class OfferController {
    @Autowired
    private OfferService offerService;
    //报价表
    @GetMapping("zjk-offer")
    public String toOfferDatagrid(){ return "before-sale/offer";}
    //报价表单
    @GetMapping("zjk-offer-form")
    public String toOfferFrom(){
        return "before-sale/offer-form";
    }
    //编辑窗口
    @GetMapping("zjk-offer-edit-form")
    public String toEditOffer(){
        return "before-sale/offer-edit-form";
    }
    //添加报价明细
    @GetMapping("add-offer-details")
    public String toOfferDetails(){
        return "before-sale/add-offer-edtails";
    }
    //编辑报价明细
    @GetMapping("zjk-offer-edit-offerDetail-form")
    public String toEditOfferDetails(){
        return "before-sale/offer-edit-offerDetail-form";
    }

    /**
     * 分页展示报价
     * @param offer
     * @param page
     * @param rows
     * @param request
     * @return
     */
    @PostMapping("zjk-showOfferList")
    @ResponseBody
    public Map showOfferList(Offer offer, int page, int rows, HttpServletRequest request){
        List<Offer> list = offerService.showOfferList(offer,page,rows,null,null,null,null);
        Long total = offerService.getOfferCount(offer,null,null,null,null);
        Map map = new HashMap();
        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    /**
     * 高级查询 报价
     * @param offer
     * @param page
     * @param rows
     * @param minTotalMoney
     * @param maxTotalMoney
     * @param updateStartTime
     * @param updateEndTime
     * @return
     */
    @PostMapping("zjk-advanceOffer")
    public Map advanceOffer(Offer offer, int page, int rows, BigDecimal minTotalMoney,
                            BigDecimal maxTotalMoney, Date updateStartTime, Date updateEndTime){
        List<Offer> list = offerService.showOfferList(offer,page,rows,minTotalMoney,maxTotalMoney,updateStartTime,updateEndTime);
        Long total = offerService.getOfferCount(offer,minTotalMoney,maxTotalMoney,updateStartTime,updateEndTime);
        Map map = new HashMap();
        map.put("rows", list);
        map.put("total", total);
        return map;
    }

    /**
     * 展示一个报价对应的报价明细
     * @param ofId
     * @return
     */
    @PostMapping("zjk-getOfferDetailsVOList")
    @ResponseBody
    public List<OfferDetailsVO> getOfferDetailsVO(Long ofId){
        Long id = ofId == null ? 0L : ofId;
        return offerService.getOfferDetailsVOList(id);
    }

    /**
     * 删除报价
     * @param ofId
     * @return
     */
    @PostMapping("zjk-deleteOfferById")
    @ResponseBody
    public Result deleteOfferById(Long ofId){
        Long id = ofId == null ? 0:new Long(ofId);
        return offerService.deleteOfferById(id);
    }

    /**
     * 添加报价
     * @param allOffer
     * @param chanceId
     * @param checkState
     * @param clientId
     * @param contactId
     * @param date
     * @param ofNo
     * @param ofType
     * @param theme
     * @param userId
     * @return
     */
    @PostMapping("zjk-addOffer")
    @ResponseBody
    public Result addOffer(BigDecimal allOffer, BigDecimal chanceId, String checkState,
                           BigDecimal clientId, BigDecimal contactId, Date date, String ofNo, String ofType, String theme, BigDecimal userId){
        try {
            return  offerService.addOffer(allOffer,chanceId,checkState,clientId,contactId,date,ofNo,ofType,theme,userId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(e);
        }


    }

    /**
     * 添加报价明细
     * @param offerDetail
     * @param ofId
     * @return
     */
    @GetMapping("add-offerDetail")
    @ResponseBody
    public Result addOfferDtail(OfferDetail offerDetail,Long ofId){
        ofId = ofId == null ? 0 : ofId;
        if (offerDetail != null) {
            offerService.addOfferDetails(offerDetail, ofId);
            return Result.SUCCESS;
        } else {
            return new Result();
        }

    }

    /**
     * 获取一条报价明细
     * （用于修改）
     * @param deId
     * @return
     */
    @PostMapping("getOneOfferDetail")
    @ResponseBody
    public OfferDetailsVO getOneOfferDetail(Long deId){
        deId = deId == null ? 0:deId;
        return offerService.getOneOfferDetail(deId);
    }

    @PostMapping("saveEditOfferDetail")
    @ResponseBody
    public Result saveEditOfferDetail(OfferDetail offerDetail){
        if(offerDetail != null){
            OfferDetail od = offerService.getOneOfferDetail(offerDetail.getDeId()).getOfferDetail();
            od.setCounts(offerDetail.getCounts());
            offerService.saveOfferDetail(od);
            return Result.SUCCESS;

        }else {
            return new Result();
        }
    }

    /**
     * 编辑报价
     * @param offer
     * @return
     */
    @PostMapping("zjk-editOffer")
    @ResponseBody
    public Result editOffer(Offer offer){
        return offerService.editOffer(offer);
    }

}
